﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using APP.HELPER.Utils.Language;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Dictionary.ERP;
using Common.Base;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Security.ERP;
using Extra.ERP;
using Common;
using System.Drawing;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.Data;
using Common.Report;

namespace Dictionary
{
    public class xucDepartment : XucBaseBasicB
    {
        public delegate void ItemSelectedEventHander(object sender, HRM_DEPARTMENT item);

        private GridColumn colBranchName;
        private GridColumn colDepartmentCode;
        private GridColumn colDepartmentName;
        private GridColumn colDescription;
        private GridColumn colPhone;
        private GridColumn colQuantity;
        private IContainer components;
        private DockManager dm;
        private GridView gbList;
        private GridControl gcList;

        public xucDepartment()
        {
            InitializeComponent();
            InitMultiLanguages();
            if (base.ParentForm != null)
            {
                dm.Form = base.ParentForm;
            }
            Init();
            base.ucToolBar.bm.SetPopupContextMenu(gcList, base.ucToolBar.pm);
            base.ucToolBar.bbiHistory.Visibility = BarItemVisibility.Always;
            base.ucToolBar.bbiUtils.Visibility = BarItemVisibility.Always;
            base.ucToolBar.bbiGroup.Visibility = BarItemVisibility.Always;
            base.ucToolBar.bbiMerge.Visibility = BarItemVisibility.Always;
            base.ucToolBar.bbiChangeId.Visibility = BarItemVisibility.Always;
            base.ucToolBar.bbiEditUnitConvret.Visibility = BarItemVisibility.Always;
        }

        public event ItemSelectedEventHander ItemSelected;

        protected override void Add()
        {
            var add = new xfmDepartmentAdd(Common.Class.Actions.Add);
            add.Added += frm_Added;
            add.ShowDialog();
        }

        public override void Change()
        {
            if (MyRule.IsAccess("bbiDepartment"))
            {
                var item = new HRM_DEPARTMENT();
                object focusedRowCellValue = gbList.GetFocusedRowCellValue("DepartmentCode");
                if (focusedRowCellValue != null)
                {
                    base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                    if (item.Get(focusedRowCellValue.ToString()) != "OK")
                    {
                        this.DoHide();
                        XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        this.DoHide();
                        var add = new xfmDepartmentAdd(Common.Class.Actions.Update, item);
                        add.Updated += frm_Updated;
                        add.Added += frm_Added;
                        add.ShowDialog();
                    }
                }
            }
        }

        public override void Delete()
        {
            if (MyRule.IsDelete("bbiDepartment"))
            {
                bool flag = false;
                int[] selectedRows = gbList.GetSelectedRows();
                if (
                    XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
                {
                    string str;
                    object rowCellValue;
                    base.SetWaitDialogCaption("Đang x\x00f3a...");
                    var hrm_department = new HRM_DEPARTMENT();
                    for (int i = selectedRows.Length; i > 0; i--)
                    {
                        flag = true;
                        rowCellValue = gbList.GetRowCellValue(selectedRows[i - 1], "DepartmentCode");
                        if (rowCellValue != null)
                        {
                            SYS_LOG.Insert("Danh S\x00e1ch Ph\x00f2ng Ban", "Xo\x00e1", rowCellValue.ToString());
                            str = hrm_department.Delete(rowCellValue.ToString());
                            if (str == "OK")
                            {
                                gbList.DeleteRow(selectedRows[i - 1]);
                            }
                            else if (str != "OK")
                            {
                                MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\r\n" + str,
                                    "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                        }
                    }
                    this.DoHide();
                    if (!flag && (hrm_department.GetList().Rows.Count != 0))
                    {
                        rowCellValue = gbList.GetFocusedRowCellValue("DepartmentCode");
                        if (rowCellValue != null)
                        {
                            SYS_LOG.Insert("Danh S\x00e1ch Ph\x00f2ng Ban", "Xo\x00e1", rowCellValue.ToString());
                            base.SetWaitDialogCaption("Đang x\x00f3a...");
                            str = hrm_department.Delete(rowCellValue.ToString());
                            if (str == "OK")
                            {
                                gbList.DeleteRow(gbList.FocusedRowHandle);
                            }
                            else if (str != "OK")
                            {
                                MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\r\n" + str,
                                    "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                            this.DoHide();
                        }
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected void DoShowMenu(GridHitInfo hi)
        {
            if (hi.HitTest == GridHitTest.ColumnButton)
            {
                var menu = new GridViewColumnButtonMenu(gbList);
                menu.Init(hi);
                menu.Show(hi.HitPoint);
            }
        }

        protected override bool ExportPermision()
        {
            base._exportView = gbList;
            if (!MyRule.IsExport("bbiDepartment"))
            {
                return false;
            }
            SYS_LOG.Insert("Danh S\x00e1ch Ph\x00f2ng Ban", "Xuất");
            return base.ExportPermision();
        }

        private void frm_Added(object sender, HRM_DEPARTMENT Item)
        {
            ReLoad();
        }

        private void frm_Updated(object sender, HRM_DEPARTMENT Item)
        {
            ReLoad();
        }

        private void gbList_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle == -2147483648)
            {
                e.Handled = true;
                e.Painter.DrawObject(e.Info);
                Rectangle bounds = e.Bounds;
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0x3f, 0xa5, 0xa5, 0)), bounds);
                bounds.Height--;
                bounds.Width--;
                e.Graphics.DrawRectangle(Pens.Blue, bounds);
            }
            int rowHandle = e.RowHandle;
            if (rowHandle >= 0)
            {
                rowHandle++;
                e.Info.DisplayText = rowHandle.ToString();
            }
        }

        private void gbList_DoubleClick(object sender, EventArgs e)
        {
            if (gbList.RowCount != 0)
            {
                Change();
            }
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            ucList_ListKeyDown(sender, e);
            if ((e.KeyCode == Keys.Delete) && (gbList.RowCount != 0))
            {
                Delete();
                SetMenu(null);
            }
        }

        private void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DoShowMenu(gbList.CalcHitInfo(new Point(e.X, e.Y)));
            }
            SetMenu(null);
        }

        private void gbList_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            var ex = new Common.Class.RowClickEventArgs(e.RowHandle, e.Column.ColumnHandle);
            this.ItemSelectd(ex);
            SetMenu(ex);
            SetSearch(ex);
        }

        private void Init()
        {
            MyRule.Check("bbiDepartment");
            base.ucToolBar.bbiPrint.Visibility = MyRule.Printed;
            base.ucToolBar.bbiExport.Visibility = MyRule.Exported;
            base.ucToolBar.bbiAdd.Visibility = MyRule.Added;
            base.ucToolBar.bbiDelete.Visibility = MyRule.Deleted;
            base.ucToolBar.bbiEdit.Visibility = MyRule.Edited;
            base.RibbonBar.Add.Visibility = MyRule.Added;
            base.RibbonBar.Change.Visibility = MyRule.Edited;
            base.RibbonBar.Delete.Visibility = MyRule.Deleted;
            base.RibbonBar.Print.Visibility = MyRule.Printed;
            base.RibbonBar.Export.Visibility = MyRule.Exported;
            base.RibbonBar.Import.Visibility = MyRule.Imported;
            gcList.DataSource = new HRM_DEPARTMENT().GetList();
        }

        private void InitializeComponent()
        {
            components = new Container();
            gcList = new GridControl();
            gbList = new GridView();
            colDepartmentCode = new GridColumn();
            colDepartmentName = new GridColumn();
            colPhone = new GridColumn();
            colBranchName = new GridColumn();
            colQuantity = new GridColumn();
            colDescription = new GridColumn();
            dm = new DockManager(components);
            gcList.BeginInit();
            gbList.BeginInit();
            dm.BeginInit();
            base.SuspendLayout();
            base.ucToolBar.Size = new Size(0x2e8, 0x2b);
            gcList.Dock = DockStyle.Fill;
            gcList.EmbeddedNavigator.Buttons.Append.Visible = false;
            gcList.EmbeddedNavigator.Buttons.CancelEdit.Visible = false;
            gcList.EmbeddedNavigator.Buttons.Edit.Visible = false;
            gcList.EmbeddedNavigator.Buttons.EndEdit.Visible = false;
            gcList.EmbeddedNavigator.Buttons.Remove.Visible = false;
            gcList.EmbeddedNavigator.TextStringFormat = "{0}/{1}";
            gcList.Location = new Point(0, 0x2b);
            gcList.MainView = gbList;
            gcList.Name = "gcList";
            gcList.Size = new Size(0x2e8, 0x1d4);
            gcList.TabIndex = 2;
            gcList.UseEmbeddedNavigator = true;
            gcList.ViewCollection.AddRange(new BaseView[] {gbList});
            gbList.Appearance.GroupRow.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            gbList.Appearance.GroupRow.Options.UseFont = true;
            gbList.Appearance.HeaderPanel.Options.UseFont = true;
            gbList.Appearance.HeaderPanel.Options.UseImage = true;
            gbList.Columns.AddRange(new[]
            {colDepartmentCode, colDepartmentName, colPhone, colBranchName, colQuantity, colDescription});
            gbList.GridControl = gcList;
            gbList.GroupCount = 1;
            gbList.GroupPanelText = "K\x00e9o v\x00e0 Thả cột v\x00e0o đ\x00e2y để nh\x00f3m dữ liệu";
            gbList.IndicatorWidth = 40;
            gbList.Name = "gbList";
            gbList.OptionsBehavior.AllowIncrementalSearch = true;
            gbList.OptionsBehavior.AutoExpandAllGroups = true;
            gbList.OptionsBehavior.CopyToClipboardWithColumnHeaders = false;
            gbList.OptionsBehavior.FocusLeaveOnTab = true;
            gbList.OptionsLayout.StoreAllOptions = true;
            gbList.OptionsLayout.StoreAppearance = true;
            gbList.OptionsMenu.EnableColumnMenu = false;
            gbList.OptionsMenu.EnableGroupPanelMenu = false;
            gbList.OptionsNavigation.EnterMoveNextColumn = true;
            gbList.OptionsSelection.InvertSelection = true;
            gbList.OptionsSelection.MultiSelect = true;
            gbList.OptionsView.AnimationType = GridAnimationType.AnimateFocusedItem;
            gbList.OptionsView.EnableAppearanceEvenRow = true;
            gbList.OptionsView.RowAutoHeight = true;
            gbList.OptionsView.ShowAutoFilterRow = true;
            gbList.SortInfo.AddRange(new[] {new GridColumnSortInfo(colBranchName, ColumnSortOrder.Ascending)});
            gbList.CustomDrawRowIndicator += gbList_CustomDrawRowIndicator;
            gbList.KeyDown += gbList_KeyDown;
            gbList.MouseDown += gbList_MouseDown;
            gbList.DoubleClick += gbList_DoubleClick;
            gbList.RowCellClick += gbList_RowCellClick;
            colDepartmentCode.Caption = "M\x00e3 ph\x00f2ng ban";
            colDepartmentCode.FieldName = "DepartmentCode";
            colDepartmentCode.Name = "colDepartmentCode";
            colDepartmentCode.OptionsColumn.AllowEdit = false;
            colDepartmentCode.OptionsColumn.ReadOnly = true;
            colDepartmentCode.Visible = true;
            colDepartmentCode.VisibleIndex = 0;
            colDepartmentName.Caption = "T\x00ean ph\x00f2ng ban";
            colDepartmentName.FieldName = "DepartmentName";
            colDepartmentName.Name = "colDepartmentName";
            colDepartmentName.OptionsColumn.AllowEdit = false;
            colDepartmentName.OptionsColumn.ReadOnly = true;
            colDepartmentName.Visible = true;
            colDepartmentName.VisibleIndex = 1;
            colPhone.Caption = "Điện thoại";
            colPhone.FieldName = "Phone";
            colPhone.Name = "colPhone";
            colPhone.OptionsColumn.AllowEdit = false;
            colPhone.OptionsColumn.ReadOnly = true;
            colPhone.Visible = true;
            colPhone.VisibleIndex = 2;
            colBranchName.Caption = "Chi nh\x00e1nh";
            colBranchName.FieldName = "BranchName";
            colBranchName.Name = "colBranchName";
            colBranchName.OptionsColumn.AllowEdit = false;
            colBranchName.OptionsColumn.ReadOnly = true;
            colBranchName.Visible = true;
            colBranchName.VisibleIndex = 2;
            colQuantity.Caption = "Số lượng";
            colQuantity.FieldName = "Quantity";
            colQuantity.Name = "colQuantity";
            colQuantity.OptionsColumn.AllowEdit = false;
            colQuantity.OptionsColumn.ReadOnly = true;
            colQuantity.Visible = true;
            colQuantity.VisibleIndex = 3;
            colDescription.Caption = "Ghi ch\x00fa";
            colDescription.FieldName = "Description";
            colDescription.Name = "colDescription";
            colDescription.OptionsColumn.AllowEdit = false;
            colDescription.OptionsColumn.ReadOnly = true;
            colDescription.Visible = true;
            colDescription.VisibleIndex = 4;
            dm.DockModeVS2005FadeSpeed = 0x3e8;
            dm.TopZIndexControls.AddRange(new[]
            {
                "DevExpress.XtraBars.BarDockControl", "DevExpress.XtraBars.StandaloneBarDockControl",
                "System.Windows.Forms.StatusBar", "DevExpress.XtraBars.Ribbon.RibbonStatusBar",
                "DevExpress.XtraBars.Ribbon.RibbonControl"
            });
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(gcList);
            base.Name = "xucDepartment";
            base.Size = new Size(0x2e8, 0x1ff);
            base.Controls.SetChildIndex(base.ucToolBar, 0);
            base.Controls.SetChildIndex(gcList, 0);
            gcList.EndInit();
            gbList.EndInit();
            dm.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            colDepartmentCode.Caption = MultiLanguages.GetString("tbl_Department", "DepartmentCode",
                colDepartmentCode.Caption);
            colDepartmentName.Caption = MultiLanguages.GetString("tbl_Department", "DepartmentName",
                colDepartmentName.Caption);
            colQuantity.Caption = MultiLanguages.GetString("tbl_Department", "Quantity", colQuantity.Caption);
            colBranchName.Caption = MultiLanguages.GetString("tbl_Department", "BranchName", colBranchName.Caption);
            colPhone.Caption = MultiLanguages.GetString("tbl_Department", "Phone", colPhone.Caption);
            colDescription.Caption = MultiLanguages.GetString("tbl_Department", "Description", colDescription.Caption);
        }

        protected override void Print()
        {
            if (MyRule.IsPrint("bbiDepartment"))
            {
                SYS_LOG.Insert("Danh S\x00e1ch Ph\x00f2ng Ban", "In");
                new xfmReport("Danh S\x00e1ch Ph\x00f2ng Ban").ShowPrintPreview(gcList);
                base.Print();
            }
        }

        private void RaiseItemSelectedEventHander(HRM_DEPARTMENT item)
        {
            if (ItemSelected != null)
            {
                ItemSelected(this, item);
            }
        }

        public override void ReLoad()
        {
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            gcList.DataSource = new HRM_DEPARTMENT().GetList();
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            gbList.ClearColumnsFilter();
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        protected override void SetMenu(Common.Class.RowClickEventArgs e)
        {
            object rowCellValue = gbList.GetRowCellValue(gbList.FocusedRowHandle, "DepartmentCode");
            base.DisableMenu(false);
            if (rowCellValue == null)
            {
                base.DisableMenu(true);
            }
            else
            {
                base.SetMenu(e);
            }
        }

        public override void SetSearch(Common.Class.RowClickEventArgs e)
        {
            if (base._search)
            {
                var item = new HRM_DEPARTMENT();
                object rowCellValue = gbList.GetRowCellValue(base.MRowClickEventArgs.RowIndex, "DepartmentCode");
                if (rowCellValue != null)
                {
                    base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                    if (item.Get(rowCellValue.ToString()) != "OK")
                    {
                        this.DoHide();
                        XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        this.DoHide();
                        RaiseItemSelectedEventHander(item);
                    }
                }
            }
        }

        private void ucList_ListKeyDown(object sender, KeyEventArgs key)
        {
            if (key.KeyCode == Keys.F2)
            {
                if (gbList.RowCount != 0)
                {
                    Change();
                }
            }
            else if ((key.KeyCode == Keys.Control) | (key.KeyCode == Keys.E))
            {
                if (gbList.RowCount != 0)
                {
                    Change();
                }
            }
            else if (key.KeyCode == Keys.Enter)
            {
                if (gbList.RowCount != 0)
                {
                    Change();
                }
            }
            else if (key.KeyCode == Keys.F5)
            {
                ReLoad();
            }
            else if ((key.KeyCode == Keys.Control) | (key.KeyCode == Keys.N))
            {
                Add();
            }
            else if ((key.KeyCode == Keys.Control) | (key.KeyCode == Keys.T))
            {
                Add();
            }
        }
    }
}